package com.activenetwork.smlite.page.rfp;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.EventFiringWebDriver;

import com.activenetwork.custom.uielement.UIElement;
import com.activenetwork.custom.uielement.decorate.InJson;
import com.activenetwork.listener.ImplicitlyWaitListener;
import com.activenetwork.smlite.page.event.EventDetailView.EventSourceSubview;
import com.activenetwork.smlite.page.generic.PageObject;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpAdditionalRequest;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpEventSpace;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpFoodBeverage;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpReview;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpSetting;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpSleepingRoom;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpVendorSearch;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.WebDriverFactory;

public class RfpBasicsSubview implements PageObject {
	
	@InJson
	private UIElement cancel;
	@InJson
	private UIElement previousStep;
	@InJson
	private UIElement nextStep;
	
	/**
	 * Click cancel button
	 * @return
	 */
	public EventSourceSubview clickCancel(){
		cancel.click();
		return PageLoader.load(EventSourceSubview.class);
	}
	/**
	 * Click next button, if on review page, it throws exception
	 * @return
	 */
	
	public RfpBasicsSubview clickNext(){
		WebDriver driver = EnvContext.webDriverTL.get();
		Class<? extends RfpBasicsSubview> clazz = findNext(driver);
		nextStep.click();	
		confirmPopup(true); // draft may display the unsaved data confirm popup
		return PageLoader.load(clazz);

	}
	/**
	 * Click previous button, if on basics page, it throws exception
	 * @return
	 */
	public RfpBasicsSubview clickPrevious(){
		WebDriver driver = EnvContext.webDriverTL.get();
		Class<? extends RfpBasicsSubview> clazz = findPrevious(driver);
		previousStep.click();
		return PageLoader.load(clazz);
	}
	
	protected void confirmPopup(boolean ok){
		WebDriver driver = EnvContext.webDriverTL.get();
		try{
			WebDriverFactory.unwrap((EventFiringWebDriver)driver, new ImplicitlyWaitListener());
			if(ok){
				driver.findElement(By.cssSelector(".gwt-PopupPanel div.modal-footer button.btn-orange")).click();
			}else{
				driver.findElement(By.cssSelector(".gwt-PopupPanel div.modal-footer button.btn:first-child")).click();
			}
		}catch(NoSuchElementException e){
			
		}finally{
			WebDriverFactory.wrap((EventFiringWebDriver)driver, new ImplicitlyWaitListener());
		}
	}
	
	protected Class<? extends RfpBasicsSubview> findNext(WebDriver driver){
		// Locate which rfp flow page it is on
		WebElement activeTab = driver.findElement(By.cssSelector("ul.nav-pills>li.active"));
		
		WebElement nextLi = null;	
		List<WebElement> siblings = activeTab.findElements(By.xpath("./following-sibling::li"));
		for(WebElement li : siblings){
			String value = li.getAttribute("class");
			if(value.equals("hide")){
				continue;
			}else{
				nextLi = li;
				break;
			}
		}
		if(nextLi == null) throw new RuntimeException("Unable to find the next unhide rfp flow page");
		
		String nextPageName = nextLi.findElement(By.xpath("./a")).getAttribute("data-rfp-maintainance-wizard-header");
		
		if(nextPageName.equals("basics")){
			throw new RuntimeException("This is impossible that next rfp flow page is basics");
		}else if(nextPageName.equals("sleepingroom")){
			return RfpSleepingRoom.class;
		}else if(nextPageName.equals("eventspace")){
			return RfpEventSpace.class;
		}else if(nextPageName.equals("foodandbeverage")){
			return RfpFoodBeverage.class;
		}else if(nextPageName.equals("additionalrequests")){
			return RfpAdditionalRequest.class;
		}else if(nextPageName.equals("vendorsearch")){
			return RfpVendorSearch.class;
		}else if(nextPageName.equals("review")){
			return RfpReview.class;
		}else{
			throw new RuntimeException("next flow page header is unknown :" + nextPageName);
		}
		
	}
	
	protected Class<? extends RfpBasicsSubview> findPrevious(WebDriver driver){
		// Locate which rfp flow page it is on
		WebElement activeTab = driver.findElement(By.cssSelector("ul.nav-pills>li.active"));
		
		WebElement previousLi = null;
		List<WebElement> siblings = activeTab.findElements(By.xpath("./preceding-sibling::li"));

		for(int i=siblings.size()-1;i>=0;i--){
			String value = siblings.get(i).getAttribute("class");
			if(value.equals("hide")){
				continue;
			}else{
				previousLi = siblings.get(i);
				break;
			}
		}
		if(previousLi == null) throw new RuntimeException("Unable to find the previous unhide rfp flow page");
		
		String previousPageName = previousLi.findElement(By.xpath("./a")).getAttribute("data-rfp-maintainance-wizard-header");
		
		if(previousPageName.equals("basics")){
			return RfpSetting.class;
		}else if(previousPageName.equals("sleepingroom")){
			return RfpSleepingRoom.class;
		}else if(previousPageName.equals("eventspace")){
			return RfpEventSpace.class;
		}else if(previousPageName.equals("foodandbeverage")){
			return RfpFoodBeverage.class;
		}else if(previousPageName.equals("additionalrequests")){
			return RfpAdditionalRequest.class;
		}else if(previousPageName.equals("vendorsearch")){
			return RfpVendorSearch.class;
		}else if(previousPageName.equals("review")){
			throw new RuntimeException("This is impossible that previous rfp flow page is review");
		}else{
			throw new RuntimeException("previous flow page header is unknown :" + previousPageName);
		}
		
	}
	


}
